/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VSLangProj80;

namespace DaveSexton.DocProject.Wizards
{
	public sealed partial class ChooseSourcesControl : UserControl
	{
		#region Public Properties
		public IEnumerable<IAnyProject> SelectedProjects
		{
			get
			{
				foreach (KeyedProjectItem item in projectsList.SelectedItems)
					yield return projects[item.Key];
			}
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
		public SourceCollection ExternalSources
		{
			get
			{
				return externalSources;
			}
			set
			{
				externalSources = value ?? new SourceCollection(project.Provider.AllSupportedExternalSourceFileExtensions);
			}
		}
		#endregion

		#region Private / Protected
		private SourceCollection externalSources;
		private readonly KeyedProjectCollection<IAnyProject> projects = new KeyedProjectCollection<IAnyProject>();
		private readonly KeyedProjectCollection<IDocProject> docProjects = new KeyedProjectCollection<IDocProject>();
		private readonly IDocProject project;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseSourcesControl" /> class.
		/// </summary>
		public ChooseSourcesControl(IDocProject project)
			: this()
		{
			if (project == null)
				throw new ArgumentNullException("project");

			this.project = project;

			externalSources = new SourceCollection(project.Provider.AllSupportedExternalSourceFileExtensions);
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseSourcesControl" /> class.
		/// </summary>
		private ChooseSourcesControl()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		public void AddProjectRange(IEnumerable<IAnyProject> sourceProjects)
		{
			if (sourceProjects == null)
				throw new ArgumentNullException("sourceProjects");

			projectsList.SuspendLayout();

			foreach (IAnyProject project in sourceProjects)
				AddProject(project);

			projectsList.ResumeLayout(true);
		}

		public void AddProject(IAnyProject sourceProject)
		{
			if (sourceProject == null)
				throw new ArgumentNullException("sourceProject");

			projects.Add(sourceProject);

			IDocProject docProject = sourceProject as IDocProject;

			if (docProject != null)
			{
				docProjects.Add(docProject);

				if (!showDocProjectsCheckbox.Checked)
				{
					if (!showDocProjectsCheckbox.Enabled)
						showDocProjectsCheckbox.Enabled = true;

					return;
				}
			}

			projectsList.Items.Add(KeyedProjectCollection<IAnyProject>.CreateKeyedProjectItem(sourceProject));
		}

		private void ManageExternalSources()
		{
			using (ExternalSourcesEditorDialog dialog = new ExternalSourcesEditorDialog(externalSources, project))
			{
				dialog.CustomSourceFilter = project.Provider.CustomExternalSourcesAssemblyOrFileDialogFilter;
				dialog.UseAssemblyFilter = project.Provider.UseExternalSourcesAssemblyDialogFilter;

				if (dialog.ShowDialog(DocProjectEnvironment.MainWindow) == DialogResult.OK && dialog.HasChanges)
					externalSources = new SourceCollection(project.Provider.AllSupportedExternalSourceFileExtensions, dialog.Sources);
			}
		}

		private void ShowDocProjects()
		{
			projectsList.SuspendLayout();

			foreach (IDocProject project in docProjects)
				projectsList.Items.Add(KeyedProjectCollection<IDocProject>.CreateKeyedProjectItem(project));

			projectsList.ResumeLayout(true);
		}

		private void HideDocProjects()
		{
			projectsList.SuspendLayout();

			for (int i = 0; i < projectsList.Items.Count; i++)
			{
				KeyedProjectItem item = (KeyedProjectItem) projectsList.Items[i];

				if (docProjects.Contains(item.Key))
				{
					projectsList.Items.Remove(item);
					i--;
				}
			}

			projectsList.ResumeLayout(true);
		}
		#endregion

		#region Event Handlers
		private void selectAllButton_Click(object sender, EventArgs e)
		{
			projectsList.SuspendLayout();

			for (int i = 0; i < projectsList.Items.Count; i++)
			{
				KeyedProjectItem item = (KeyedProjectItem) projectsList.Items[i];

				if (!projectsList.SelectedItems.Contains(item))
					projectsList.SelectedItems.Add(item);
			}

			projectsList.ResumeLayout(true);
		}

		private void showDocProjectsCheckbox_CheckedChanged(object sender, EventArgs e)
		{
			if (showDocProjectsCheckbox.Checked)
				ShowDocProjects();
			else
				HideDocProjects();
		}

		private void externalSourceButton_Click(object sender, EventArgs e)
		{
			ManageExternalSources();
		}
		#endregion
	}
}
